package com.bigdata.service.mysql.impl;

import com.bigdata.model.Result;
import com.bigdata.model.SearchModel;
import com.bigdata.mysql.dao.JkfwRoleMapper;
import com.bigdata.mysql.dao.JkfwRoleMenuMapper;
import com.bigdata.mysql.dao.JkfwUserRoleMapper;
import com.bigdata.mysql.entity.*;
import com.bigdata.service.mysql.JkfwRoleService;
import com.bigdata.service.mysql.base.impl.BaseService;
import com.bigdata.utils.StringUtil;
import com.bigdata.utils.shiro.ShiroUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * JkfwRole的服务接口的实现类
 *
 * @author
 */
@Service
public class JkfwRoleServiceImpl extends BaseService<JkfwRoleMapper, JkfwRole, JkfwRoleExample> implements JkfwRoleService {
    private final Logger log = LogManager.getLogger(this.getClass());

    @Autowired
    JkfwRoleMenuMapper jkfwRoleMenuMapper;
    @Autowired
    JkfwUserRoleMapper jkfwUserRoleMapper;

    @Override
    public Result queryPage(SearchModel searchModel) {
        Page<JkfwRole> page = PageHelper.startPage(searchModel.getPageNumber(), searchModel.getPageSize());
        JkfwRoleExample example = new JkfwRoleExample();
        JkfwRoleExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(searchModel.getS_name())) {
            criteria.andRoleNameLike("%" + searchModel.getS_name() + "%");
        }
        dao.selectByExample(example);
        Result res = Result.pageOk(page.getResult());
        res.setTotal(page.getTotal());
        return res;
    }

    @Override
    public boolean ckRoleName(String id, String rolename) {
        if (StringUtils.isNoneBlank(id)) {
            JkfwRole role = dao.selectByPrimaryKey(id);
            role.getRoleName().equals(rolename);
            return true;
        }
        JkfwRoleExample example = new JkfwRoleExample();
        example.createCriteria().andRoleNameEqualTo(rolename);
        List<JkfwRole> list = dao.selectByExample(example);
        if (list.size() > 0) {
            return false;
        }
        return true;
    }

    @Override
    @Transactional(readOnly = false)
    public Result addRole(JkfwRole role, String menus) {
        JkfwRoleExample example = new JkfwRoleExample();
        example.createCriteria().andRoleNameEqualTo(role.getRoleName());
        List<JkfwRole> roles = dao.selectByExample(example);
        if (roles.size() == 0) {
            role.setCreateTime(new Date());
            role.setCreateUserId(ShiroUtils.getSysUser().getUserId());
            role.setRoleId(UUID.randomUUID().toString().replace("-", ""));
            dao.insert(role);
            List<String> ss = StringUtil.splitList(menus);
            for (String s : ss) {
                JkfwRoleMenu roleMenu = new JkfwRoleMenu();
                roleMenu.setId(UUID.randomUUID().toString().replace("-", ""));
                roleMenu.setRoleId(role.getRoleId());
                roleMenu.setMenuId(s);
                jkfwRoleMenuMapper.insert(roleMenu);
            }
        } else {
            return Result.fail("角色名称已存在！");
        }
        return Result.ok();
    }

    public boolean ckRoleName(String name) {
        JkfwRoleExample example = new JkfwRoleExample();
        example.createCriteria().andRoleNameEqualTo(name);
        List<JkfwRole> list = dao.selectByExample(example);
        if (list.size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * @param role
     * @return com.bigdata.model.Result
     * @Author lly
     * @Date 2019/7/24 0024
     * @Description 修改角色
     */
    @Override
    @Transactional(readOnly = false)
    public Result updateRole(JkfwRole role, String menus) {
        JkfwRole jkrole = dao.selectByPrimaryKey(role.getRoleId());
        if (jkrole == null) {
            return Result.fail("您要修改数据不存在！");
        } else {
            boolean istrue = false;
            if (role.getRoleName().equals(jkrole.getRoleName())) {
                istrue = true;
            } else {
                if (ckRoleName(role.getRoleName())) {
                    istrue = true;
                } else {
                    return Result.fail("您要修改的角色名称已存在！");
                }
            }
            if (istrue) {
                dao.updateByPrimaryKeySelective(role);
                JkfwRoleMenuExample example = new JkfwRoleMenuExample();
                example.createCriteria().andRoleIdEqualTo(role.getRoleId());
                jkfwRoleMenuMapper.deleteByExample(example);
                List<String> ss = StringUtil.splitList(menus);
                if (ss.size() > 0) {
                    for (String s : ss) {
                        JkfwRoleMenu roleMenu = new JkfwRoleMenu();
                        roleMenu.setId(UUID.randomUUID().toString().replace("-", ""));
                        roleMenu.setRoleId(role.getRoleId());
                        roleMenu.setMenuId(s);
                        jkfwRoleMenuMapper.insert(roleMenu);
                    }
                }
            }
        }
        return Result.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public Result delRole(String id) {
//删除用户角色 删除角色和模块
        JkfwRoleMenuExample example = new JkfwRoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(id);
        jkfwRoleMenuMapper.deleteByExample(example);
        JkfwUserRoleExample example1 = new JkfwUserRoleExample();
        example1.createCriteria().andRoleIdEqualTo(id);
        jkfwUserRoleMapper.deleteByExample(example1);
        dao.deleteByPrimaryKey(id);
        return Result.ok();
    }
}